package com.shell.provider;

import com.shell.bee.base.utils.JsonUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.governor.modules.po.PubBizEnt;
import com.shell.governor.modules.service.PubBizEntService;
import com.shell.governor.modules.vo.BizEntVo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PubBizEntProvider
{
  @Autowired
  private PubBizEntService pubBizEntService;
  
  @RequestMapping({"/pub-biz-ent/saveOrUpdateBizEnt"})
  public void saveOrUpdateBizEnt(@RequestParam("entId") String entId, @RequestParam(value="bizIds", required=false) String bizIds)
  {
    this.pubBizEntService.deleteBizEntByEntId(entId);
    if (StringUtil.emptyAndNull(bizIds)) {
      return;
    }
    String[] ids = bizIds.split(",");
    for (String bizId : ids)
    {
      PubBizEnt bizEnt = new PubBizEnt();
      bizEnt.setEntId(entId);
      bizEnt.setBizId(bizId);
      this.pubBizEntService.save(bizEnt);
    }
  }
  
  @RequestMapping({"/pub-biz-ent/saveOrUpdateBizEntBatch"})
  public void saveOrUpdateBizEntBatch(@RequestParam("bizEntJson") String bizEntListJson)
  {
    List<BizEntVo> bizEntVos = null;
    try
    {
      bizEntVos = JsonUtil.fromJsonArray(bizEntListJson, BizEntVo.class);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    for (BizEntVo bizEntVo : bizEntVos)
    {
      String entId = bizEntVo.getEntId();
      String bizIds = bizEntVo.getBizIds();
      
      this.pubBizEntService.deleteBizEntByEntId(entId);
      if (StringUtil.emptyAndNull(bizIds)) {
        return;
      }
      String[] ids = bizIds.split(",");
      for (String bizId : ids)
      {
        PubBizEnt bizEnt = new PubBizEnt();
        bizEnt.setEntId(entId);
        bizEnt.setBizId(bizId);
        this.pubBizEntService.save(bizEnt);
      }
    }
  }
}
